home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / tools / packer / ha0999beta / src / haio.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  3KB  |  164 lines

  1. /***********************************************************************
  2.   This file is part of HA, a general purpose file archiver.
  3.   Copyright (C) 1995 Harri Hirvola
  4.  
  5.   This program is free software; you can redistribute it and/or modify
  6.   it under the terms of the GNU General Public License as published by
  7.   the Free Software Foundation; either version 2 of the License, or
  8.   (at your option) any later version.
  9.  
  10.   This program is distributed in the hope that it will be useful,
  11.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.   GNU General Public License for more details.
  14.  
  15.   You should have received a copy of the GNU General Public License
  16.   along with this program; if not, write to the Free Software
  17.   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ************************************************************************
  19.        HA I/O routines
  20. ***********************************************************************/
  21.  
  22. #include <stdio.h>
  23. #include "ha.h"
  24. #include "haio.h"
  25. #include "error.h"
  26. #include "haio.h"
  27.  
  28. #define uppdcrc(_crc,_c) _crc=(crctab[((int)(_crc)^(_c))&0xff]^((_crc)>>8))
  29. #define CRCMASK        0xffffffffUL
  30. #define CRCP        0xEDB88320UL
  31.  
  32. int infile,outfile;
  33. U32B crc;
  34. U32B crctab[256];
  35. unsigned char ib[BLOCKLEN],ob[BLOCKLEN];
  36. int ibl,ibf,obl;
  37. U32B icnt,ocnt,totalsize;
  38. unsigned char r_crc,w_crc,r_progdisp,w_progdisp;
  39. static int write_on,crctabok=0;
  40. static char *outname=NULL,*inname=NULL;
  41.  
  42. void (*outspecial)(unsigned char *obuf, unsigned oblen);
  43. unsigned (*inspecial)(unsigned char *ibuf, unsigned iblen);
  44.  
  45. static void makecrctab(void) {
  46.  
  47.     U16B i,j;
  48.     U32B tv;
  49.     
  50.     for (i=0;i<256;i++) {
  51.     tv=i;
  52.     for (j=8;j>0;j--) {
  53.         if (tv&1) tv=(tv>>1)^CRCP;
  54.         else tv>>=1;
  55.     }
  56.     crctab[i]=tv;
  57.     }
  58. }
  59.  
  60. void setoutput(int fh, int mode, char *name) {
  61.  
  62.     outname=name;
  63.     outspecial=NULL;
  64.     if (fh>=0) write_on=1;
  65.     else write_on=0;
  66.     obl=0;
  67.     ocnt=0;
  68.     outfile=fh;
  69.     w_crc=mode&CRCCALC;
  70.     if (w_crc) {
  71.     if (!crctabok) makecrctab();
  72.     crc=CRCMASK;
  73.     }
  74.     w_progdisp=mode&PROGDISP;
  75. }
  76.  
  77.  
  78. void setinput(int fh, int mode, char *name) {
  79.  
  80.     inname=name;
  81.     inspecial=NULL;
  82.     ibl=0;
  83.     icnt=0;
  84.     infile=fh;
  85.     r_crc=mode&CRCCALC;
  86.     if (r_crc) {
  87.     if (!crctabok) makecrctab();
  88.     crc=CRCMASK;
  89.     }
  90.     r_progdisp=mode&PROGDISP;
  91. }
  92.  
  93.  
  94. U32B getcrc(void) {
  95.     
  96.     return crc^CRCMASK;
  97. }
  98.  
  99. void clearcrc(void) {
  100.  
  101.     crc=CRCMASK;
  102. }
  103.  
  104. void bread(void) {
  105.     
  106.     register S16B i;
  107.     register unsigned char *ptr;
  108.  
  109.     if (inspecial!=NULL) {
  110.     ibl=(*inspecial)(ib,BLOCKLEN);
  111.     ibf=0;
  112.     return;
  113.     }
  114.     else {
  115.     ibl=read(infile,ib,BLOCKLEN);
  116.     if (ibl<0) error(1,ERR_READ,inname);
  117.     ibf=0;
  118.     }
  119.     if (ibl) {
  120.     icnt+=ibl;
  121.     if (r_progdisp) {
  122.         printf("%3d %%\b\b\b\b\b",
  123.            (int)(icnt*100/(totalsize==0?1:totalsize)));
  124.         fflush(stdout);
  125.     }
  126.     if (r_crc) {
  127.         for (i=0,ptr=ib;i<ibl;++i) {
  128.         uppdcrc(crc,*(ptr++));
  129.         }
  130.     }
  131.     }
  132. }
  133.  
  134. void bwrite(void) {
  135.  
  136.     register S16B i;
  137.     register unsigned char *ptr;
  138.  
  139.     if (obl) {
  140.     if (outspecial!=NULL) {
  141.         (*outspecial)(ob,obl);
  142.     }
  143.     else {
  144.         if (write_on && write(outfile,ob,obl)!=obl) 
  145.           error(1,ERR_WRITE,outname);
  146.         ocnt+=obl;
  147.         if (w_progdisp) {
  148.         printf("%3d %%\b\b\b\b\b",
  149.                (int)(ocnt*100/(totalsize==0?1:totalsize)));
  150.         fflush(stdout);
  151.         }
  152.         if (w_crc) {
  153.         for (i=0,ptr=ob;i<obl;++i) {
  154.             uppdcrc(crc,*(ptr++));
  155.         }
  156.         }
  157.     }
  158.     obl=0;
  159.     }
  160. }
  161.  
  162.  
  163.  
  164.